<%
    Users user = Mill.getUser(request.getParameter("uid"));
	wernercon.mill.logistics.si.Allocation task = user.logistics.si.allocation;

	user.getRequest().setRequest(request);
	user.getResponse().setResponse(response,out);
	
	if (!user.Authenticated()) {
		response.sendRedirect("logon.jsp");
		return;
	}
	((Tasks)task.getOwner()).clearFocus();
	task.setFocus(true);	


	task.doTask();
	Record ref = task.getSI_Ref();
%>

<%@include file="header.jsp"%>

<link href="style.css" rel="stylesheet" type="text/css">

<script language="javascript">

function tonsInputValid(i)
{
	var allocated_tons = parseFloat(getValue("allocated_tons_" + i));
	var stock_tons = parseFloat(getText("stock_tons_" + i + "_"));
	if (allocated_tons <= 0) {
		alert("Allocated tons can not be equal or less than zero.");
		return false;
	}
	if (allocated_tons > stock_tons) {
		alert("Allocated tons can not be greater than stock tons.");
		return false;
	}
	return true;
}

function tonsAllocatedChanged(i)
{
	var kg_per_bag     = parseFloat(getValue("kg_per_bag_" + i));
	var allocated_tons = parseFloat(getValue("allocated_tons_" + i));
	var stock_tons = parseFloat(getText("stock_tons_" + i + "_"));
	if (kg_per_bag > 0) {
		var allocated_bags = (allocated_tons*1000 + kg_per_bag - 1) / kg_per_bag;
		setValue("allocated_bags_" + i, formatNumber(allocated_bags,0,false));
	}
}

function doAllocate(i,contract_allocated_id)
{
	if (tonsInputValid(i)) {
		setValue("contract_allocated_id",contract_allocated_id);
		setValue("allocated_tons",getValue("allocated_tons_" + i));		
		setValue("allocated_bags",getValue("allocated_bags_" + i));		
		doTask(1);
	}
}

var row1 = 0;
function row1Clicked(i)
{
	show("ok_" + i);
	//if (row1 > 0) hide("ok_" + row1);
	row1 = i;
	highlightOn(getObj("row1_" + i));
}

function doDelete(id)
{
	if (confirm("Are you sure to delete this item?")) {
		setValue("allocated_id", id);
		doTask(2);
	}
}

</script>

<form method="POST" name="formMain" action="" onSubmit="">
<table width="100%"  border="0" cellpadding="0" cellspacing="1" class="style2">
  <tr>
    <td colspan="3"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="2%"><img src="../shared/images/backicon.gif" width="18" height="18" title="Back" onClick="doLink('<%=task.getLinkPage()%>')"></td>
        <td width="20%"><img src="../shared/images/contracts-allocation.jpg" width="300" height="24"></td>
        <td width="78%"><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
          <tr>
            <td width="19%">Shipping Instruction </td>
            <td width="50%"><strong><%=ref.getString("ref_number")%></strong></td>
            <td width="10%">Packing</td>
            <td width="21%"><strong><%=ref.getString("packing")%></strong></td>
          </tr>
          <tr>
            <td>Commodity</td>
            <td><strong><%=ref.getString("origin")+" "+ref.getString("quality")+" Coffee "+ref.getString("grade")%></strong></td>
            <td>Quantity</td>
            <td><strong><%=ref.getFloatValue("tons") + "Mts / " +ref.getIntValue("no_of_bags") + " Bags"%></strong></td>
          </tr>
          <tr>
            <td>Origin</td>
            <td><strong><%=ref.getString("origin")%></strong></td>
            <td>Quality</td>
            <td><strong><%=ref.getString("quality")%></strong></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    </tr>
  <tr bgcolor="#EEEEEE">
    <td width="13%" align="center">SI Ref </td>
    <td width="24%" align="right">Select Grade&nbsp;</td>
    <td width="63%"><select name="grade_id" id="grade_id" class="style2" style="width:100%;" onChange="doPost();">
      <option value="0">All</option>
      <%task.getResponse().setOption(task.getGrade_List());%>
      <%task.getResponse().setSelected("grade_id");%>
    </select></td>
  </tr>
  <tr>
    <td><table width="100%"  border="0">
      <tr valign="top">
        <td><select name="inst_id" size=23 class="style2" id="inst_id" style="width:100%;" onChange="doPost();">
            <%task.getResponse().setOptionX(task.getSI_List());%>
            <%task.getResponse().setSelected("inst_id");%>
        </select></td>
      </tr>
    </table></td>
    <td colspan="2" valign="top"><table width="100%"  border="1" cellpadding="0" cellspacing="0" class="style2">
      <tr>
        <td><div style="height:200px; overflow:scroll">
		<table width="100%"  border="0" cellpadding="0" cellspacing="1" class="style2">
          <tr bgcolor="#EEEEEE" align="center" style="font-weight:bold">
            <td width="3%" rowspan="2">No</td>
            <td width="10%" rowspan="2">Contract Ref </td>
            <td width="8%" rowspan="2">Broker<br>No</td>
            <td width="15%" rowspan="2">Supplier</td>
            <td width="21%" rowspan="2">Grade</td>
            <td height="16" colspan="2">Contract</td>
            <td colspan="2">Unallocated</td>
            <td colspan="2">Allocate</td>
            <td width="2%" rowspan="2">OK</td>
            </tr>
          <tr bgcolor="#EEEEEE" align="center">
            <td width="7%">Tons</td>
            <td width="6%">Bags</td>
            <td width="6%">Tons</td>
            <td width="8%">Bags</td>
            <td width="7%">Mts</td>
            <td width="7%">Bags</td>
          </tr>
<%
	float total_contract_tons = 0;
	float total_contract_bags = 0;
	float total_stock_tons = 0;
	float total_stock_bags = 0;
	
	ResultSet rs = task.getContract_UnAllocated();
	while (rs.next()) {
		int i = rs.getRow();
		float contract_tons = rs.getFloat("tons");
		float allocated_tons = rs.getFloat("allocated_tons");
		float stock_tons = contract_tons - allocated_tons;
		
		int contract_bags  = rs.getInt("no_of_bags");
		int allocated_bags = rs.getInt("allocated_bags");
		int stock_bags = contract_bags - allocated_bags;
		
		total_contract_tons += contract_tons;
		total_contract_bags += contract_bags;
		total_stock_tons += stock_tons;
		total_stock_bags += stock_bags;
%>
	<input type="hidden" name="kg_per_bag_<%=i%>" id="kg_per_bag_<%=i%>"  value="<%=rs.getFloat("kg_per_bag")%>">

          <tr id="row1_<%=i%>" class="style11" onClick="row1Clicked(<%=i%>);">
            <td align="center" bgcolor="#EEEEEE"><%=i%></td>
            <td><%=rs.getString("ref_number")%></td>
            <td align="center"><%=rs.getString("contract_no")%></td>
            <td><%=rs.getString("supplier")%></td>
            <td><%=rs.getString("grade")%></td>
            <td align="right"><%=Utils.numberToStr(contract_tons,1)%> &nbsp;</td>
            <td align="right"><%=Utils.numberToStr(contract_bags,0)%> &nbsp;</td>
            <td align="right"><label id="stock_tons_<%=i%>_"><%=Utils.numberToStr(stock_tons,1)%></label> &nbsp;</td>
            <td align="right"><%=stock_bags%> &nbsp;</td>
            <td align="center"><input type="text" name="allocated_tons_<%=i%>" id="allocated_tons_<%=i%>" class="style2" style="width:60px; text-align:center" value="" onChange="tonsAllocatedChanged(<%=i%>)"></td>
            <td align="center"><input type="text" name="allocated_bags_<%=i%>" id="allocated_bags_<%=i%>" class="style2" style="width:60px; text-align:center" value=""></td>
            <td align="center"><img src="images/update.gif" name="ok_<%=i%>" id="ok_<%=i%>" style="display:" width="15" height="15" onClick="doAllocate(<%=i%>,<%=rs.getInt("id")%>)"></td>
            </tr>
<%
	}
	rs.close();
%>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td align="right">&nbsp;</td>
            <td align="right"><strong>Total</strong></td>
            <td align="right"><strong><%=Utils.numberToStr(total_contract_tons,1)%></strong></td>
            <td align="right"><strong><%=Utils.numberToStr(total_contract_bags,0)%></strong></td>
            <td align="right"><strong><%=Utils.numberToStr(total_stock_tons,1)%></strong></td>
            <td align="right"><strong><%=Utils.numberToStr(total_stock_bags,0)%></strong></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
        </table>
		</div></td>
      </tr>
      <tr>
        <td><div style="height:150px; overflow:scroll">
          <table width="100%"  border="0" cellpadding="0" cellspacing="1" class="style2">
            <tr bgcolor="#EEEEEE" align="center" style="font-weight:bold">
              <td width="3%" rowspan="2">No</td>
              <td width="13%" rowspan="2">Contract Ref </td>
              <td width="10%" rowspan="2">Broker<br>No</td>
              <td width="22%" rowspan="2">Supplier</td>
              <td width="28%" rowspan="2">Grade</td>
              <td colspan="2">Allocated</td>
              <td width="3%" rowspan="2">Del</td>
              </tr>
            <tr bgcolor="#EEEEEE" align="center">
              <td width="9%">Mts</td>
              <td width="12%">Bags</td>
              </tr>
<%
	rs = task.getContract_Allocated();
	float total_allocated_tons = 0;
	int total_allocated_bags = 0;
	while (rs.next()) {
		int i = rs.getRow();
		float allocated_tons = rs.getFloat("allocated_tons");
		int   allocated_bags = rs.getInt("allocated_bags");
		total_allocated_tons += allocated_tons;
		total_allocated_bags += allocated_bags;
%>
            <tr class="style11" onClick="highlightOn(this,1);">
              <td align="center"><%=i%></td>
              <td><%=rs.getString("ref_number")%></td>
              <td align="center"><%=rs.getString("contract_no")%></td>
              <td><%=rs.getString("supplier")%></td>
              <td><%=rs.getString("grade")%></td>
              <td align="right"><%=Utils.numberToStr(allocated_tons,1)%> &nbsp;</td>
              <td align="right"><%=Utils.numberToStr(allocated_bags,0)%> &nbsp;</td>
              <td align="center"><img src="../shared/images/delete.gif" width="15" height="15" onClick="doDelete(<%=rs.getInt("id")%>);"></td>
              </tr>
<%
	}
%>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td align="right">&nbsp;</td>
              <td align="right">&nbsp;</td>
              <td align="right">&nbsp;</td>
              <td align="right">&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td align="right">&nbsp;</td>
              <td align="right"><strong>Total</strong></td>
              <td align="right"><strong><%=Utils.numberToStr(total_allocated_tons,1)%></strong> &nbsp;</td>
              <td align="right"><strong><%=Utils.numberToStr(total_allocated_bags,0)%></strong> &nbsp;</td>
              <td>&nbsp;</td>
              </tr>
          </table>
        </div></td>
      </tr>
    </table></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td align="right"><img src="images/cardview.jpg" width="55" height="18" onClick="doLink('logistics.si.jsp')"></td>
  </tr>
</table>

	<input type="hidden" name="uid"     id="uid"     value="<%=user.getuid()%>">
	<input type="hidden" name="task_id" id="task_id"  value="0">
	<input type="hidden" name="view"    id="view"     value="1">
	<input type="hidden" name="contract_id" id="contract_id"  value="<%=task.si.contract_id%>">
	<input type="hidden" name="contract_allocated_id" id="contract_allocated_id"  value="0">
	<input type="hidden" name="allocated_id" id="allocated_id"  value="0">
	<input type="hidden" name="allocated_tons" id="allocated_tons"  value="0">
	<input type="hidden" name="allocated_bags" id="allocated_bags"  value="0">
</form>

<%@include file="footer.jsp"%>



